﻿using UnityEngine;
using System.Collections.Generic;
using System;
using System.Diagnostics;
using System.Collections;
using System.Text;
using Object = System.Object;

public class GLogger
{
    
    public static ArrayList LMTstrList = new ArrayList();
    public static bool LMTlock = true;
    private string moduleName;
    private GLogType minLogType = GLogType.All;

    public GLogger(string moduleName)
    {
        this.moduleName = moduleName;
    }
    /// <summary>
    /// 设置输出日志的最小级别
    /// </summary>
    /// <param name="gt"></param>
    public void SetLogLevel(GLogType minLogType)
    {
        this.minLogType = minLogType;
    }
    
    /// <summary>
    /// 调试信息，发布版会屏蔽，编译时根据宏定义直接忽略对应调用代码
    /// </summary>
    /// <param name="msg"></param>
    [Conditional("DEBUG")]
    public void Debug(object msg)
    {
        if (minLogType > GLogType.Debug)
            return;
        Debug(msg.ToString());
    }

    [Conditional("DEBUG")]
    public void Debug(String msg)
    {
        if (minLogType > GLogType.Debug)
            return;
        msg = StringTools.Format(msg, moduleName);
        LogAgent.log(GLogType.Debug, msg);
    }

    [Conditional("DEBUG")]
    public void Debug(string format, params object[] args)
    {
        if (minLogType > GLogType.Debug)
            return;
        string msg = StringTools.Format(format, moduleName, args);
        LogAgent.log(GLogType.Debug, msg);
    }

    [Conditional("DEBUG")]
    public void Debug(string format, string args0)
    {
        if (minLogType > GLogType.Debug)
            return;
        string msg = StringTools.Format(format, moduleName, args0);
        LogAgent.log(GLogType.Debug, msg);
    }

    [Conditional("DEBUG")]
    public void Debug(string format, string args0, string args1)
    {
        if (minLogType > GLogType.Debug)
            return;
        string msg = StringTools.Format(format, moduleName, args0, args1);
        LogAgent.log(GLogType.Debug, msg);
    }

    [Conditional("DEBUG")]
    public void Debug(string format, string args0, string args1, string args2)
    {
        if (minLogType > GLogType.Debug)
            return;
        string msg = StringTools.Format(format, moduleName, args0, args1, args2);
        LogAgent.log(GLogType.Debug, msg);
    }

    [Conditional("DEBUG")]
    public void Debug(string format, string args0, string args1, string args2, string args3)
    {
        if (minLogType > GLogType.Debug)
            return;
        string msg = StringTools.Format(format, moduleName, args0, args1, args2, args3);
        LogAgent.log(GLogType.Debug, msg);
    }
    
    [Conditional("DEBUG")]
    public void Debug(string format, string args0, string args1, string args2, string args3, string args4)
    {
        if (minLogType > GLogType.Debug)
            return;
        string msg = StringTools.Format(format, moduleName, args0, args1, args2, args3, args4);
        LogAgent.log(GLogType.Debug, msg);
    }

    [Conditional("DEBUG")]
    public void Debug(string format, string args0, string args1, string args2, string args3, string args4, string args5)
    {
        if (minLogType > GLogType.Debug)
            return;
        string msg = StringTools.Format(format, moduleName, args0, args1, args2, args3, args4, args5);
        LogAgent.log(GLogType.Debug, msg);
    }

    public void Info(object msg)
    {
        if (minLogType > GLogType.Info)
            return;
        
        Info(msg.ToString());
    }

    public void Info(String msg)
    {
        if (minLogType > GLogType.Info)
            return;
        msg = StringTools.Format(msg, moduleName);
        LogAgent.log(GLogType.Info, msg);
    }

    public void Info(string format, params object[] args)
    {
        if (minLogType > GLogType.Info)
            return;
        string msg = StringTools.Format(format, moduleName, args);
        LogAgent.log(GLogType.Info, msg);
    }

    public void Info(string format, string args0)
    {
        if (minLogType > GLogType.Info)
            return;
        string msg = StringTools.Format(format, moduleName, args0);
        LogAgent.log(GLogType.Info, msg);
    }

    public void Info(string format, string args0, string args1)
    {
        if (minLogType > GLogType.Info)
            return;
        string msg = StringTools.Format(format, moduleName, args0, args1);
        LogAgent.log(GLogType.Info, msg);
    }

    public void Info(string format, string args0, string args1, string args2)
    {
        if (minLogType > GLogType.Info)
            return;
        string msg = StringTools.Format(format, moduleName, args0, args1, args2);
        LogAgent.log(GLogType.Info, msg);
    }

    public void Info(string format, string args0, string args1, string args2, string args3)
    {
        if (minLogType > GLogType.Info)
            return;
        string msg = StringTools.Format(format, moduleName, args0, args1, args2, args3);
        LogAgent.log(GLogType.Info, msg);
    }

    public void Info(string format, string args0, string args1, string args2, string args3, string args4)
    {
        if (minLogType > GLogType.Info)
            return;
        string msg = StringTools.Format(format, moduleName, args0, args1, args2, args3, args4);
        LogAgent.log(GLogType.Info, msg);
    }
    
    public void Info(string format, string args0, string args1, string args2, string args3, string args4, string args5)
    {
        if (minLogType > GLogType.Info)
            return;
        string msg = StringTools.Format(format, moduleName, args0, args1, args2, args3, args4, args5);
        LogAgent.log(GLogType.Info, msg);
    }

    public void Warning(object msg)
    {
        if (minLogType > GLogType.Warning)
            return;
        
        Warning(msg.ToString());
    }

    public void Warning(String msg)
    {
        if (minLogType > GLogType.Warning)
            return;
        msg = StringTools.Format(msg, moduleName);
        LogAgent.log(GLogType.Warning, msg);
    }

    public void Warning(string format, params object[] args)
    {
        if (minLogType > GLogType.Warning)
            return;
        string msg = StringTools.Format(format, moduleName, args);
        LogAgent.log(GLogType.Warning, msg);
    }

    public void Warning(string format, string args0)
    {
        if (minLogType > GLogType.Warning)
            return;
        string msg = StringTools.Format(format, moduleName, args0);
        LogAgent.log(GLogType.Warning, msg);
    }

    public void Warning(string format, string args0, string args1)
    {
        if (minLogType > GLogType.Warning)
            return;
        string msg = StringTools.Format(format, moduleName, args0, args1);
        LogAgent.log(GLogType.Warning, msg);
    }

    public void Warning(string format, string args0, string args1, string args2)
    {
        if (minLogType > GLogType.Warning)
            return;
        string msg = StringTools.Format(format, moduleName, args0, args1, args2);
        LogAgent.log(GLogType.Warning, msg);
    }

    public void Warning(string format, string args0, string args1, string args2, string args3)
    {
        if (minLogType > GLogType.Warning)
            return;
        string msg = StringTools.Format(format, moduleName, args0, args1, args2, args3);
        LogAgent.log(GLogType.Warning, msg);
    }

    public void Warning(string format, string args0, string args1, string args2, string args3, string args4)
    {
        if (minLogType > GLogType.Warning)
            return;
        string msg = StringTools.Format(format, moduleName, args0, args1, args2, args3, args4);
        LogAgent.log(GLogType.Warning, msg);
    }
    
    public void Warning(string format, string args0, string args1, string args2, string args3, string args4, string args5)
    {
        if (minLogType > GLogType.Warning)
            return;
        string msg = StringTools.Format(format, moduleName, args0, args1, args2, args3, args4, args5);
        LogAgent.log(GLogType.Warning, msg);
    }

    public void Error(object msg)
    {
        if (minLogType > GLogType.Error)
            return;
        
        Error(msg.ToString());
    }

    public void Error(String msg)
    {
        if (minLogType > GLogType.Error)
            return;
        msg = StringTools.Format(msg, moduleName);
        LogAgent.log(GLogType.Error, msg);
    }

    public void Error(string format, params object[] args)
    {
        if (minLogType > GLogType.Error)
            return;
        string msg = StringTools.Format(format, moduleName, args);
        LogAgent.log(GLogType.Error, msg);
    }

    public void Error(string format, string args0)
    {
        if (minLogType > GLogType.Error)
            return;
        string msg = StringTools.Format(format, moduleName, args0);
        LogAgent.log(GLogType.Error, msg);
    }

    public void Error(string format, string args0, string args1)
    {
        if (minLogType > GLogType.Error)
            return;
        string msg = StringTools.Format(format, moduleName, args0, args1);
        LogAgent.log(GLogType.Error, msg);
    }

    public void Error(string format, string args0, string args1, string args2)
    {
        if (minLogType > GLogType.Error)
            return;
        string msg = StringTools.Format(format, moduleName, args0, args1, args2);
        LogAgent.log(GLogType.Error, msg);
    }

    public void Error(string format, string args0, string args1, string args2, string args3)
    {
        if (minLogType > GLogType.Error)
            return;
        string msg = StringTools.Format(format, moduleName, args0, args1, args2, args3);
        LogAgent.log(GLogType.Error, msg);
    }

    public void Error(string format, string args0, string args1, string args2, string args3, string args4)
    {
        if (minLogType > GLogType.Error)
            return;
        string msg = StringTools.Format(format, moduleName, args0, args1, args2, args3, args4);
        LogAgent.log(GLogType.Error, msg);
    }

    public void Error(string format, string args0, string args1, string args2, string args3, string args4, string args5)
    {
        if (minLogType > GLogType.Error)
            return;
        string msg = StringTools.Format(format, moduleName, args0, args1, args2, args3, args4, args5);
        LogAgent.log(GLogType.Error, msg);
    }

    public void Exception(object msg)
    {
        if (minLogType > GLogType.Exception)
            return;
        Exception(msg.ToString());
    }

    public void Exception(String msg)
    {
        if (minLogType > GLogType.Exception)
            return;
        msg = StringTools.Format(msg, moduleName);
        LogAgent.log(GLogType.Exception, msg);
    }
    
    public void Exception(string format, params object[] args)
    {
        if (minLogType > GLogType.Exception)
            return;
        string msg = StringTools.Format(format, moduleName, args);
        LogAgent.log(GLogType.Exception, msg);
    }

    public void Exception(string format, string args0)
    {
        if (minLogType > GLogType.Exception)
            return;
        string msg = StringTools.Format(format, moduleName, args0);
        LogAgent.log(GLogType.Exception, msg);
    }

    public void Exception(string format, string args0, string args1)
    {
        if (minLogType > GLogType.Exception)
            return;
        string msg = StringTools.Format(format, moduleName, args0, args1);
        LogAgent.log(GLogType.Exception, msg);
    }

    public void Exception(string format, string args0, string args1, string args2)
    {
        if (minLogType > GLogType.Exception)
            return;
        string msg = StringTools.Format(format, moduleName, args0, args1, args2);
        LogAgent.log(GLogType.Exception, msg);
    }

    public void Exception(string format, string args0, string args1, string args2, string args3)
    {
        if (minLogType > GLogType.Exception)
            return;
        string msg = StringTools.Format(format, moduleName, args0, args1, args2, args3);
        LogAgent.log(GLogType.Exception, msg);
    }

    public void Exception(string format, string args0, string args1, string args2, string args3, string args4)
    {
        if (minLogType > GLogType.Exception)
            return;
        string msg = StringTools.Format(format, moduleName, args0, args1, args2, args3, args4);
        LogAgent.log(GLogType.Exception, msg);
    }

    public void Exception(string format, string args0, string args1, string args2, string args3, string args4, string args5)
    {
        if (minLogType > GLogType.Exception)
            return;
        string msg = StringTools.Format(format, moduleName, args0, args1, args2, args3, args4, args5);
        LogAgent.log(GLogType.Exception, msg);
    }
}

public class GLog
{
    //默认的logger，无模块名
    private static GLogger gLogger = new GLogger(null);
    private static Dictionary<string, GLogger> gLoggers = new Dictionary<string, GLogger>();

    public static ArrayList LMTstrList = new ArrayList();
    public static bool LMTlock = false;
    /// <summary>
    /// 设置输出日志的最小级别
    /// </summary>
    /// <param name="gt"></param>
    public static void SetLogLevel(GLogType minLogType)
    {
        gLogger.SetLogLevel(minLogType);
    }
    
    public static GLogger GetLogger(Object obj,GLogType minLogType=GLogType.All)
    {
        return GetLogger(obj.GetType().Name,minLogType);
    }

    public static GLogger GetLogger(string moduleName,GLogType minLogType=GLogType.All)
    {
        if (string.IsNullOrEmpty(moduleName))
            return gLogger;
        GLogger log;
        if (!gLoggers.TryGetValue(moduleName, out log))
        {
            log = new GLogger(moduleName);
            gLoggers.Add(moduleName, log);
        }
        log.SetLogLevel(minLogType);
        return log;
    }

    /// <summary>
    /// 调试信息，发布版会屏蔽，编译时根据宏定义直接忽略对应调用代码
    /// </summary>
    /// <param name="msg"></param>
    [Conditional("DEBUG")]
    public static void Debug(object msg)
    {
        gLogger.Debug(msg);
    }

    [Conditional("DEBUG")]
    public static void Debug(String msg)
    {
        gLogger.Debug(msg);
    }

    [Conditional("DEBUG")]
    public static void Debug(string format, params object[] args)
    {
        gLogger.Debug(format, args);
    }

    [Conditional("DEBUG")]
    public static void Debug(string format, string args0)
    {
        gLogger.Debug(format, args0);
    }

    [Conditional("DEBUG")]
    public static void Debug(string format, string args0, string args1)
    {
        gLogger.Debug(format, args0, args1);
    }

    [Conditional("DEBUG")]
    public static void Debug(string format, string args0, string args1, string args2)
    {
        gLogger.Debug(format, args0, args1, args2);
    }

    [Conditional("DEBUG")]
    public static void Debug(string format, string args0, string args1, string args2, string args3)
    {
        gLogger.Debug(format, args0, args1, args2, args3);
    }

    [Conditional("DEBUG")]
    public static void Debug(string format, string args0, string args1, string args2, string args3, string args4)
    {
        gLogger.Debug(format, args0, args1, args2, args3, args4);
    }

    [Conditional("DEBUG")]
    public static void Debug(string format, string args0, string args1, string args2, string args3, string args4, string args5)
    {
        gLogger.Debug(format, args0, args1, args2, args3, args4, args5);
    }

    public static void Info(object msg)
    {
#if UNITY_EDITOR //写到控制台
        if (LMTlock)
        {
            LMTstrList.Add(msg);
        }
#endif
        gLogger.Info(msg);
    }public static void Info(String msg)
    {
        
#if UNITY_EDITOR //写到控制台
        if (LMTlock)
        {
            LMTstrList.Add(msg);
        }
#endif
        gLogger.Info(msg);
    }

    public static void Info(string format, params object[] args)
    {
        gLogger.Info(format, args);
    }

    public static void Info(string format, string args0)
    {
        gLogger.Info(format, args0);
    }

    public static void Info(string format, string args0, string args1)
    {
        gLogger.Info(format, args0, args1);
    }

    public static void Info(string format, string args0, string args1, string args2)
    {
        gLogger.Info(format, args0, args1, args2);
    }

    public static void Info(string format, string args0, string args1, string args2, string args3)
    {
        gLogger.Info(format, args0, args1, args2, args3);
    }

    public static void Info(string format, string args0, string args1, string args2, string args3, string args4)
    {
        gLogger.Info(format, args0, args1, args2, args3, args4);
    }

    public static void Info(string format, string args0, string args1, string args2, string args3, string args4, string args5)
    {
        gLogger.Info(format, args0, args1, args2, args3, args4, args5);
    }

    public static void Warning(object msg)
    {
        gLogger.Warning(msg);
    }

    public static void Warning(String msg)
    {
        gLogger.Warning(msg);
    }

    public static void Warning(string format, params object[] args)
    {
        gLogger.Warning(format, args);
    }

    public static void Warning(string format, string args0)
    {
        gLogger.Warning(format, args0);
    }

    public static void Warning(string format, string args0, string args1)
    {
        gLogger.Warning(format, args0, args1);
    }

    public static void Warning(string format, string args0, string args1, string args2)
    {
        gLogger.Warning(format, args0, args1, args2);
    }

    public static void Warning(string format, string args0, string args1, string args2, string args3)
    {
        gLogger.Warning(format, args0, args1, args2, args3);
    }

    public static void Warning(string format, string args0, string args1, string args2, string args3, string args4)
    {
        gLogger.Warning(format, args0, args1, args2, args3, args4);
    }

    public static void Warning(string format, string args0, string args1, string args2, string args3, string args4, string args5)
    {
        gLogger.Warning(format, args0, args1, args2, args3, args4, args5);
    }

    public static void Error(object msg)
    {
        if (msg is Exception)
        {
            gLogger.Error(((Exception)msg).Message);//直接传入的是个Exception 则只输出Message，否者msg中会附带上堆栈，导致堆栈重复
            return;
        }
        gLogger.Error(msg);
    }

    public static void Error(String msg)
    {
        gLogger.Error(msg);
    }

    public static void Error(string format, params object[] args)
    {
        gLogger.Error(format, args);
    }

    public static void Error(string format, string args0)
    {
        gLogger.Error(format, args0);
    }

    public static void Error(string format, string args0, string args1)
    {
        gLogger.Error(format, args0, args1);
    }

    public static void Error(string format, string args0, string args1, string args2)
    {
        gLogger.Error(format, args0, args1, args2);
    }

    public static void Error(string format, string args0, string args1, string args2, string args3)
    {
        gLogger.Error(format, args0, args1, args2, args3);
    }

    public static void Error(string format, string args0, string args1, string args2, string args3, string args4)
    {
        gLogger.Error(format, args0, args1, args2, args3, args4);
    }

    public static void Error(string format, string args0, string args1, string args2, string args3, string args4, string args5)
    {
        gLogger.Error(format, args0, args1, args2, args3, args4, args5);
    }


    public static void Exception(object msg)
    {
        gLogger.Exception(msg);
    }

    public static void Exception(String msg)
    {
        gLogger.Exception(msg);
    }

    public static void Exception(string format, params object[] args)
    {
        gLogger.Exception(format, args);
    }

    public static void Exception(string format, string args0)
    {
        gLogger.Exception(format, args0);
    }

    public static void Exception(string format, string args0, string args1)
    {
        gLogger.Exception(format, args0, args1);
    }

    public static void Exception(string format, string args0, string args1, string args2)
    {
        gLogger.Exception(format, args0, args1, args2);
    }

    public static void Exception(string format, string args0, string args1, string args2, string args3)
    {
        gLogger.Exception(format, args0, args1, args2, args3);
    }

    public static void Exception(string format, string args0, string args1, string args2, string args3, string args4)
    {
        gLogger.Exception(format, args0, args1, args2, args3, args4);
    }

    public static void Exception(string format, string args0, string args1, string args2, string args3, string args4, string args5)
    {
        gLogger.Exception(format, args0, args1, args2, args3, args4, args5);
    }

    #region 系统调用逻辑

    /// <summary>
    /// 进程退出
    /// </summary>
    public static void OnApplicationQuit()
    {
        LogAgent.OnApplicationQuit();
    }

    public static void Init(IGameLogCallBack gameLogCB)
    {
        LogAgent.Init(gameLogCB);
    }
    public static bool SetPause(bool bPause)
    {
        return LogAgent.SetPause(bPause);
    }
    
    public static string GetLogPath()
    {
        return LogAgent.GetLogPath();
    }
    
    public static string GetLogDirectoryPath()
    {
        return LogAgent.GetLogDirectoryPath();
    }
    
    public static void AddLogPathInfo(string appendInfo)
    {
        LogAgent.AddLogPathInfo(appendInfo);
    }
    #endregion
}

